package com.cndw.kungfu.config;

import java.net.InetAddress;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.RecoverableDataAccessException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;

import com.cndw.asyncsync.SyncServerBase;
import com.cndw.kungfu.event.once.SystemCloudAlarm;
import com.cndw.kungfu.ext.CommUtil;
import com.cndw.kungfu.ext.LogUtil;

public class ConstantsSystem {
	/**
	 * 启动时间
	 */
	private static int startTime;
	

	/**
	 * 服务器开启时间
	 */
	private static int powerCloseTime;

	/**
	 * 服务器关闭时间
	 */
	private static int powerOpenTime;

	/**
	 * 服务器关闭状态 1启动了任务 2 开始了 4 完成了
	 */
	private static int closeState = 0;

	private static final AtomicInteger sysDbErrTimes = new AtomicInteger(0);
	public static String hostAddress;
	public static String hostName;
	
	public static void init(){
		try {
			InetAddress addr = InetAddress.getLocalHost();
			hostAddress=addr.getHostAddress().toString();//获得本机IP
			hostName=addr.getHostName().toString();//获得本机名称			
		} catch (Exception e) {
			try {
				hostAddress = CommUtil.getAddress().getHostAddress();
				hostName = CommUtil.getAddress().getHostName();
			} catch (Exception e2) {
				e2.printStackTrace();
				LogUtil.getMain().error(" err on got hostname ");
			}
			
			//e.printStackTrace();
		}
	}

	public static int getPowerCloseTime() {
		return powerCloseTime;
	}

	public static void setPowerCloseTime(int powerCloseTime) {
		ConstantsSystem.powerCloseTime = powerCloseTime;
	}

	public static int getPowerOpenTime() {
		return powerOpenTime;
	}

	public static void setPowerOpenTime(int powerOpenTime) {
		ConstantsSystem.powerOpenTime = powerOpenTime;
	}

	public static int getCloseState() {
		return closeState;
	}

	public static void setCloseState(int closeState) {
		ConstantsSystem.closeState = closeState;
	}

	public static int getStartTime() {
		return startTime;
	}

	public static void setStartTime(int startTime) {
		ConstantsSystem.startTime = startTime;
	}
		

	public static String getHostAddress() {
		return hostAddress;
	}

	public static String getHostName() {
		return hostName;
	}

	public static void resetDbErrTimes() {
		sysDbErrTimes.set(0);
		if (SyncServerBase.isStopOnDBErr()) {			
			SyncServerBase.setStopOnDBErr(false);
			LogUtil.getMain().info("fantadust stopOnDbErr reset ... ");
		}
	}

	public static boolean isErrDB() {
		return sysDbErrTimes.get() >= 3;
	}

	public static void throwExceptionDB(Exception e) {
		if (e == null) {
			return;
		}
		if (e instanceof DataAccessResourceFailureException) {
			sysDbErrTimes.set(100);
			LogUtil.getMain().error(e.getMessage());
		} else if (e instanceof RecoverableDataAccessException) {
			sysDbErrTimes.incrementAndGet();
			LogUtil.getMain().error(e.getMessage());
		}else if (e instanceof CannotGetJdbcConnectionException) {
			sysDbErrTimes.set(100);
			LogUtil.getMain().error(e.getMessage());
		}else {
			e.printStackTrace();
		}
		if (sysDbErrTimes.get() >= 3) {
			if (!SyncServerBase.isStopOnDBErr()) {
				LogUtil.getMain().info("fantadust stopOnDbErr ... ");
				SystemCloudAlarm.send("a_c", SystemCloudAlarm.db_c);
			}
			SyncServerBase.setStopOnDBErr(true);			
		}
	}

}
